package test;

import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import test.Test;
import user.User;

@SuppressWarnings("serial")
public class LogInBuilder extends JFrame implements ActionListener{
	private JTextField name;
	private JPasswordField pass;
	JButton btnLogIn, btnRegister;

	public LogInBuilder() {
		//JFrame frame = new JFrame("Window Builder Test");
		setSize(300,200);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container cont = getContentPane();
		cont.setLayout(null);
		
		/*JPanel panel = new JPanel();
		panel.setBounds(0, 127, 0, 0);
		cont.add(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));*/
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(30, 13, 60, 25);
		cont.add(lblName);
		
		name = new JTextField();
		name.setBounds(120, 14, 116, 22);
		cont.add(name);
		name.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(30, 51, 80, 25);
		cont.add(lblPassword);
		
		pass = new JPasswordField();
		pass.setColumns(10);
		pass.setBounds(120, 52, 116, 22);
		cont.add(pass);
		
		btnLogIn = new JButton("Log In");
		btnLogIn.setBounds(30, 102, 97, 25);
		cont.add(btnLogIn);
		
		btnRegister = new JButton("Register");
		btnRegister.setBounds(139, 102, 97, 25);
		cont.add(btnRegister);
		
		btnLogIn.addActionListener(this);
		btnRegister.addActionListener(this);
		setVisible(true);
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		
		repaint();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		@SuppressWarnings("unused")
		User user;
		String usr,pw;
		JButton src = (JButton)e.getSource();
		usr=name.getText();
		pw=new String(pass.getPassword());
		//System.out.println(PWCrypt.scramble(pw));
		//System.out.println(PWCrypt.unscramble(PWCrypt.scramble(pw)));
		if(src==btnLogIn)
			user=Test.getUser(usr,pw);
		else if(src==btnRegister)
			Test.addUser(new User(usr,pw));
	}
}